package com.pan.basecommon.log;

import android.util.Log;

import com.pan.basecommon.utils.FileUtils;
import com.pan.basecommon.utils.TimeUtils;
import com.tencent.mars.xlog.Xlog;

import java.io.File;
import java.text.ParseException;
import java.util.List;

public class XLog {

    private static String LOG_PATH = "";
    private static String LOG_FILE = "";

    public static void loadLibrary() {
        System.loadLibrary("stlport_shared");
        System.loadLibrary("marsxlog");
    }

    public static void openXlog(String logPath, String logFile) {
        String subDir = logPath;
        String subFile = logFile;
        try {
            subDir = logPath + TimeUtils.longToString(System.currentTimeMillis(), TimeUtils.Format.yyyyMMdd);
            subFile = logFile + "_" + TimeUtils.longToString(System.currentTimeMillis(), TimeUtils.Format.yyyymmddhhmmssSSS);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        File logDir = new File(subDir);
        if (!logDir.exists()) {
            FileUtils.makeDir(subDir);
        }
        LOG_PATH = subDir;
        LOG_FILE = subFile;
        Xlog.appenderOpen(Xlog.LEVEL_VERBOSE, Xlog.AppednerModeAsync, "", subDir, subFile, "");
        Xlog.setConsoleLogOpen(false);
        com.tencent.mars.xlog.Log.setLogImp(new Xlog());
    }


    public static File closeXlog() {
        com.tencent.mars.xlog.Log.appenderClose();
        List<File> logList = FileUtils.orderFilesByPrefixAndSuffix(LOG_PATH, LOG_FILE, ".xlog");
        if (logList.size() > 0) {
            return logList.get(0);
        }
        return null;
    }

    public static void i(String tag, String msg) {
        com.tencent.mars.xlog.Log.i(tag, msg);
        Log.i(tag, msg);
    }

    public static void e(String tag, String msg) {
        com.tencent.mars.xlog.Log.e(tag, msg);
        Log.e(tag, msg);
    }

    public static void d(String tag, String msg) {
        com.tencent.mars.xlog.Log.d(tag, msg);
        Log.d(tag, msg);
    }

    public static void v(String tag, String msg) {
        com.tencent.mars.xlog.Log.v(tag, msg);
        Log.v(tag, msg);
    }

    public static void w(String tag, String msg) {
        com.tencent.mars.xlog.Log.w(tag, msg);
        Log.w(tag, msg);
    }

    public static void i(String tag, String function, String msg) {
        com.tencent.mars.xlog.Log.i(tag, "[" + function + "] - " + msg);
        Log.i(tag, "[" + function + "] - " + msg);
    }

    public static void e(String tag, String function, String msg) {
        com.tencent.mars.xlog.Log.e(tag, "[" + function + "] - " + msg);
        Log.e(tag, "[" + function + "] - " + msg);
    }

    public static void d(String tag, String function, String msg) {
        com.tencent.mars.xlog.Log.d(tag, "[" + function + "] - " + msg);
        Log.d(tag, "[" + function + "] - " + msg);
    }

    public static void v(String tag, String function, String msg) {
        com.tencent.mars.xlog.Log.v(tag, "[" + function + "] - " + msg);
        Log.v(tag, "[" + function + "] - " + msg);
    }

    public static void w(String tag, String function, String msg) {
        com.tencent.mars.xlog.Log.w(tag, "[" + function + "] - " + msg);
        Log.w(tag, "[" + function + "] - " + msg);
    }
}
